﻿Imports Controle
Public Class frmConsultaLig

#Region "Variáveis"
    Private prvIDCliente As Int32
    Private prvIDStatus As Int32
    Private prvIDUsuario As Int32
    Private prvIDClienteSel As Int32
    Private prvIDContatoSel As Int32
    Private prvData As Date
    Private prvIDOcorrencia As Int32

    Dim objCliente As New ctrCliente
    Dim objRegiao As New ctrRegiao
    Dim objStatus As New ctrStatus
    Dim objOcorrencias As New ctrOcorrencias
    Dim objUsuarios As New ctrUsuario

#End Region

#Region "Enumerações"

    Private Enum eColLigacoes
        eOcorrencia = 0
        eData = 1
        eIdCliente = 2
        eNomeCliente = 3
        eIdContato = 4
        eNomeContato = 5
        eStatus = 6
        eIDUsuario = 7
        eUsuario = 8

    End Enum
#End Region

#Region "Métodos privados"

    Private Sub FormataDtGrid()

        Dim Colunas As New DataGridViewTextBoxColumn()
        Try
            With dtGridLigacoes
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .AutoGenerateColumns = False
                .RowHeadersVisible = False

                .Columns.Clear()
                'define cada coluna do grid

                Dim Col_Ocorrencia As New DataGridViewTextBoxColumn()
                With Col_Ocorrencia
                    .DataPropertyName = "idOcorrencia"
                    .HeaderText = "Ocorrência"
                    .Width = 75
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                    .Visible = True
                End With
                .Columns.Add(Col_Ocorrencia)

                Dim Col_Data As New DataGridViewTextBoxColumn()
                With Col_Data
                    .DataPropertyName = "DataInicio"
                    .HeaderText = "Data"
                    .Width = 75
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                    .Visible = True
                    .ValueType = System.Type.GetType("Date")
                End With
                .Columns.Add(Col_Data)

                Dim Col_IDCliente As New DataGridViewTextBoxColumn()
                With Col_IDCliente
                    .DataPropertyName = "idCliente"
                    .HeaderText = "IDCliente"
                    .Width = 15
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .Visible = False
                End With
                .Columns.Add(Col_IDCliente)

                Dim Col_Cliente As New DataGridViewTextBoxColumn()
                With Col_Cliente
                    .DataPropertyName = "razaoSocial"
                    .HeaderText = "Cliente"
                    .Width = 170
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .Visible = True
                End With
                .Columns.Add(Col_Cliente)

                Dim Col_IDContato As New DataGridViewTextBoxColumn()
                With Col_IDContato
                    .DataPropertyName = "idContato"
                    .HeaderText = "IDContato"
                    .Width = 15
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .Visible = False
                End With
                .Columns.Add(Col_IDContato)

                Dim Col_Contato As New DataGridViewTextBoxColumn()
                With Col_Contato
                    .DataPropertyName = "nomeContato"
                    .HeaderText = "Contato"
                    .Width = 118
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .Visible = True
                End With
                .Columns.Add(Col_Contato)

                Dim Col_Status As New DataGridViewTextBoxColumn()
                With Col_Status
                    .DataPropertyName = "Status"
                    .HeaderText = "Status"
                    .Width = 80
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
                    .Visible = True
                End With
                .Columns.Add(Col_Status)

                Dim Col_IDUsuario As New DataGridViewTextBoxColumn()
                With Col_IDUsuario
                    .DataPropertyName = "idUsuario"
                    .HeaderText = "IDUsuario"
                    .Width = 15
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .Visible = False
                End With
                .Columns.Add(Col_IdUsuario)

                Dim Col_Usuario As New DataGridViewTextBoxColumn()
                With Col_Usuario
                    .DataPropertyName = "nomeUsuario"
                    .HeaderText = "Atendente"
                    .Width = 100
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .Visible = True
                End With
                .Columns.Add(Col_Usuario)

                .Refresh()

            End With
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub PreencherDtGrid()
        Dim dtbLigacoes As New DataTable
        Dim dtInicio As Date
        Dim dtFim As Date

        Try
            If (chkDatas.Checked = False) Then
                dtInicio = CDate(dtDataInicio.Text)
                dtFim = CDate(dtDataFim.Text)
            Else
                dtInicio = Date.MinValue
                dtFim = Date.MinValue
            End If

            Call FormataDtGrid()

            dtbLigacoes = Nothing
            dtGridLigacoes.DataSource = dtbLigacoes
            dtbLigacoes = objOcorrencias.RetornaContatosRealizados(prvIDCliente, prvIDStatus, prvIDUsuario, dtInicio, dtFim)

            If Not dtbLigacoes Is Nothing AndAlso dtbLigacoes.Rows.Count > 0 Then
                dtGridLigacoes.DataSource = dtbLigacoes
            End If
            lblNLigacoes.Text = dtGridLigacoes.RowCount

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub

#End Region


    Private Sub frmAgendaVisita_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated

        Call PreencherDtGrid()

    End Sub

    Private Sub frmAgendaVisita_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Call FormataDtGrid()

    End Sub

    Private Sub cmdFiltrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFiltrar.Click

        Call PreencherDtGrid()

    End Sub

    Private Sub cmdProcCliente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcCliente.Click
        Dim sql As String
        Dim frmDialogo As New frmPesquisa
        sql = Me.objCliente.SqlConsulta(False)

        prvIDCliente = 0

        Limpar(Me)

        With frmDialogo
            .Campos = "iIDCliente,sRazaoSocial"
            .Descricoes = "Código,Cliente"
            .Larguras = "50,300"
            .Sql = sql
            .Titulo = "Pesquisa Cliente"
            .CarregaTela()
            If .DS.Tables(0).Rows.Count > 0 Then
                .ShowDialog()
                If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                    Me.Codigo = .Id
                    Me.prvIDCliente = .Id
                    Me.txtIDCliente.Text = .Id
                    txtNomeCliente.Text = objCliente.selecionarCliente(prvIDCliente).Item("RazaoSocial")
                    Me.modoAtual = eModo.eAguardar
                    chkClientes.Checked = False
                End If
            Else
                MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                Me.prvIDCliente = 0
                MyBase.Limpar(Me)
                Exit Sub
            End If

        End With
    End Sub

    Private Sub cmdProcRegiao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcRegiao.Click
        Dim sql As String
        Dim frmDialogo As New frmPesquisa

        Try

            sql = Me.objStatus.sqlConsulta

            With frmDialogo
                .Campos = "iIDStatus,sDescricao"
                .Descricoes = "Código,Status"
                .Larguras = "50,300"
                .Sql = sql
                .Titulo = "Pesquisa Status"
                .CarregaTela()
                If .DS.Tables(0).Rows.Count > 0 Then
                    .ShowDialog()
                    If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                        Me.prvIDStatus = .Id
                        Me.txtIDStatus.Text = prvIDStatus
                        Me.txtStatus.Text = objStatus.retornarCampoStatus("Descricao", prvIDStatus).Item("Descricao").ToString
                        chkStatus.Checked = False
                    End If
                Else
                    MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                End If
            End With

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub

    Private Sub chkClientes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkClientes.CheckedChanged
        If (chkClientes.Checked = True) Then
            prvIDCliente = 0
            txtIDCliente.Text = ""
            txtNomeCliente.Text = ""
        End If
    End Sub

    Private Sub chkRegiao_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkStatus.CheckedChanged
        If (chkStatus.Checked = True) Then
            prvIDStatus = 0
            txtIDStatus.Text = ""
            txtStatus.Text = ""
        End If
    End Sub

    Private Sub chkDatas_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDatas.CheckedChanged

        If (chkDatas.Checked = True) Then
            dtDataInicio.Enabled = False
            dtDataFim.Enabled = False
        Else
            dtDataInicio.Enabled = True
            dtDataFim.Enabled = True
        End If

    End Sub

    Private Sub cmdProcurarUsuario_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcurarUsuario.Click
        Dim sql As String
        Dim frmDialogo As New frmPesquisa

        sql = Me.objUsuarios.sqlConsulta

        With frmDialogo
            .Campos = "iIDUsuario,sNome"
            .Descricoes = "Código,Nome"
            .Larguras = "50,300"
            .Sql = sql
            .Titulo = "Pesquisa de usuários"
            .CarregaTela()
            If .DS.Tables(0).Rows.Count > 0 Then
                .ShowDialog()
                If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                    prvIDUsuario = .Id
                    Me.txtNomeUsuario.Text = objUsuarios.retornarCampoUsuario("Nome", prvIDUsuario).Item("Nome")
                    chkUsuarios.Checked = False
                End If
            Else
                MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                Exit Sub
            End If

        End With
    End Sub

    Private Sub chkUsuarios_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkUsuarios.CheckedChanged

        If (chkUsuarios.Checked = True) Then
            prvIDUsuario = 0
        End If

    End Sub
    Private Sub dtGridLigacoes_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtGridLigacoes.CellDoubleClick

        Try
            Dim frmOcorr As New frmOcorrencia

            prvIDOcorrencia = CInt(dtGridLigacoes.Rows(e.RowIndex).Cells(eColLigacoes.eOcorrencia).Value)
            frmOcorr.IDOCorrencia = prvIDOcorrencia

            frmOcorr.ShowDialog()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub
End Class
